//
//  ClothesItemView.m
//  W2MD
//
//  Created by zhudongwei on 14/11/18.
//  Copyright (c) 2014年 zhudongwei. All rights reserved.
//

#import "ClothesItemView.h"
#import "SXModel.h"


@interface ClothesItemView()
{
    UIImageView *editIV;
    UIImageView *bgView;
    BOOL isEdit;
}



@end


@implementation ClothesItemView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.clicked=NO;
        
        bgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 50, 50)];
        [bgView setImage:[UIImage imageNamed:@"filter_itemquan.png"]];
        [self addSubview:bgView];
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.frame = CGRectMake(10, 0, 50, 50);
        [self.btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        UILongPressGestureRecognizer *longges = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
        [self addGestureRecognizer:longges];
        [self addSubview:self.btn];
        
        self.lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 53, 70, 17)];
        [self addSubview:self.lbl];
        self.lbl.textAlignment = NSTextAlignmentCenter;
        self.lbl.textColor = [UIColor lightGrayColor];
        self.lbl.font = [UIFont systemFontOfSize:14.];
        editIV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 0, 17, 17)];
        [editIV setImage:[UIImage imageNamed:@"filter_delete.png"]];
        [self addSubview:editIV];
        editIV.hidden = YES;
        _cstype = EM_CST_None;
        
    }
    return self;
}

-(IBAction)btnClick:(id)sender
{
    if (!isEdit) {
        switch (_cstype) {
            case EM_CST_None:
            {
                [self.clothesIVDelegate itemClickWith:self];
                _cstype = EM_CST_Clicked;
                [self showBtnStatusHL:YES];
            }
                break;
            default:
                break;
        }
    }
    else{
        [self.clothesIVDelegate itemDelete:self];
    }

    
}
-(void)setDefaultClick
{
    [self.clothesIVDelegate itemClickWith:self];
    _cstype = EM_CST_Clicked;
    [self showBtnStatusHL:YES];
}
-(IBAction)longPressAction:(id)sender
{
    NSLog(@"----------------- longPressAction");
    [self.clothesIVDelegate itemLongPressWith:self];
}

-(void)showEditIcon:(BOOL)flag
{
    if (flag) {
        isEdit = YES;
        editIV.hidden = NO;
    }
    else
    {
        isEdit = NO;
        editIV.hidden = YES;
    }
}

-(void)showBtnStatusHL:(BOOL)flag
{
    if (flag) {
        [bgView setImage:[UIImage imageNamed:@"hl_filter_itemquan.png"]];
        self.lbl.textColor = [UIColor colorWithRed:162.f/255.f green:103.f/255.f blue:215.f/255.f alpha:1.f];
        [self.btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[self.infodic objectForKey:@"Icon"]]] forState:UIControlStateNormal];
        
        NSArray *clothesArr=[NSArray arrayWithObjects:@"skirt",@"p_doujin",@"p_pijian",@"p_weijin",@"p_xiaofangjin",@"k_dadi",@"k_niuzai",@"k_xiuxian",@"k_xizhuang",@"k_yundong",@"w_mian",@"w_si", nil];
        BOOL jubu=YES;
        for (NSString *str in clothesArr) {
            if ([str isEqualToString:[self.infodic objectForKey:@"Icon"]]) {
                SXModel *model=[SXModel shareModel];
                model.juBuPeiSe=NO;
                jubu=NO;
                break;
            }
        }
        
        if (jubu) {
            SXModel *model=[SXModel shareModel];
            model.juBuPeiSe=YES;
        }
    
    }
    else
    {
        _cstype = EM_CST_None;
        [bgView setImage:[UIImage imageNamed:@"filter_itemquan.png"]];
        self.lbl.text = [self.infodic objectForKey:@"Name"];
        self.lbl.textColor = [UIColor grayColor];
        [self.btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"hb_%@.png",[self.infodic objectForKey:@"Icon"]]] forState:UIControlStateNormal];
    }
}


@end
